<!-- 投诉 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="投诉标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入投诉标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入投诉人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row class="mb8">
      <el-col :span="1.5" v-if="userType == $store.state.constants.userType_company">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >我要投诉</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange" border>
      <el-table-column label="投诉内容" align="center" prop="content" />
      <el-table-column label="创建人名称" align="center" prop="userName" width="200"/>
      <!--<el-table-column label="星级打分" align="center" prop="star" >-->
        <!--<template slot-scope="scope">-->
          <!--<el-rate v-model="scope.row.star" disabled show-score/>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['tws:question:remove']"
            v-if="scope.row.isFinish == 0"
          >回复</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: green"
            @click="handleView(scope.row)"
            v-hasPermi="['tws:question:remove']"
            v-if="scope.row.isFinish == 1"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改咨询投诉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="投诉标题" prop="title">-->
          <!--<el-input v-model="form.title" placeholder="请输入投诉标题" />-->
        <!--</el-form-item>-->
        <el-form-item label="投诉内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入投诉内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--回复对话框-->
    <el-dialog :title="title" :visible.sync="itemvisible" width="500px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px" >
        <!--<el-form-item label="投诉标题">-->
          <!--{{itemData.title}}-->
        <!--</el-form-item>-->
        <el-form-item label="投诉人">
          {{itemData.userName}} <span style="color:#333;font-size:12px;margin-left:20px">{{itemData.createTime}}</span>
        </el-form-item>
        <el-form-item label="投诉内容">
          {{itemData.content}}
        </el-form-item>
        <el-form-item label="回复内容"  v-if="itemData.isFinish == 0">
          <el-input type="textarea" v-model="form.content" placeholder="请输入回复内容"/>
        </el-form-item>
        <el-form-item  v-if="itemData.isFinish == 0">
          <el-button @click="submitAnswer" type="primary">回 复</el-button>
        </el-form-item>
        <el-form-item label="平台回复" v-if="itemData.isFinish == 1">
          {{itemData.answer}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAnswer">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion, answer } from "@/api/tws/question";
import Editor from '@/components/Editor';

export default {
  name: "Question",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 禁用弹窗新增或修改的确定按钮
      disabled: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 咨询投诉表格数据
      questionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      itemvisible:false,
      itemData:[],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        title: null,
        content: null,
        type: null,
        driverId:null,
        companyId:null,
        createBy: null,
        delFlag: null,
        status: null,
        userName: null,
        star: null,
        isFinish:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content: [
          { required: true, message: "投诉内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.userType = this.$store.state.user.type;
    this.getList();
  },
  methods: {
    /** 查询咨询投诉列表 */
    getList() {
      this.loading = true;
      this.queryParams.type = 2;
      this.queryParams.delFlag = 0;
      if (this.userType == this.$store.state.constants.userType_company){
        this.queryParams.createBy = this.$store.state.user.userId;
        listQuestion(this.queryParams).then(response => {
          this.questionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }else {
        listQuestion(this.queryParams).then(response => {
          this.questionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //取消回复
    cancelAnswer(){
      this.itemvisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        type: null,
        driverId:null,
        companyId:null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        status: 0,
        userName: null,
        star: null,
        isFinish:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "投诉";
    },
    /** 回复按钮操作 */
    handleView(row){
      this.reset();
      const id = row.id || this.ids;
      getQuestion(id).then(res => {
        this.itemData = res.data;
        this.itemvisible = true;
        this.title = "回复";
      })

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuestion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改咨询投诉";
      });
    },
    /** 回复按钮 */
    submitAnswer(){
      if (!this.form.content){
        this.$message.warning("请输入回复内容");
        return;
      }
      this.itemData.answer = this.form.content;
      answer(this.itemData).then(()=>{
        this.itemvisible=false;
        this.getList();
        this.form.content = '';
        this.reset();
        this.$message.success("回复成功!")
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.disabled = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuestion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.disabled =false;
              this.open = false;
              this.getList();
            }).catch(err => {
              this.disabled =false;
            })
          } else {
            this.form.type = 2;
            this.form.status = 1;
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.disabled =false;
              this.open = false;
              this.getList();
            }).catch(err => {
              this.disabled =false;
            })
          }
        } else {
          this.disabled = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除咨询投诉编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delQuestion(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(err => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/question/export', {
        ...this.queryParams
      }, `tws_question.xlsx`)
    }
  }
};
</script>
